<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item label="讲师名">
        <el-input
          v-model="form.name"
          placeholder="讲师名"
          style="width: 120px"
        ></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select
          v-model="form.level"
          placeholder="讲师头衔"
          style="width: 140px"
        >
          <el-option label="普通讲师" value="0"></el-option>
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-col :span="11">
          <el-form-item>
            <el-date-picker
              type="date"
              placeholder="选择开始日期"
              v-model="form.start"
              style="width: 180px; margin-right: 10px"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item>
            <el-date-picker
              type="date"
              placeholder="选择结束时间"
              v-model="form.end"
              style="width: 180px; margin-left: 10px"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" stripe border>
      <el-table-column type="index" width="50" />
      <el-table-column prop="name" label="姓名" width="80" />
      <el-table-column label="头衔" width="80">
        <template #default="scope">
          <span v-if="scope.row.level === 0">普通讲师</span>
          <span v-else-if="scope.row.level === 1">高级讲师</span>
          <span v-else-if="scope.row.level === 2">首席讲师</span>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="资历" />
      <el-table-column prop="sort" label="排序" width="60" />
      <el-table-column prop="gmtCreate" label="添加时间" width="160" />
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <!-- <pre>{{ scope.row }}</pre> -->
          <router-link :to="`/Teacher/add/${scope.row.id}`">
            <el-button size="mini" type="primary">修改</el-button>
          </router-link>
          <el-button
            size="mini"
            type="danger"
            @click="deleteTeacher(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      v-model:currentPage="current"
      :page-size="size"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script setup lang="ts">
import { getList, deleteById } from "@/api/teacher";
import { ElMessage } from "element-plus";
import { onMounted, ref, watch } from "vue";

const current = ref(1);
const size = ref(5);
const total = ref(0);
const tableData = ref([]);
const form = ref({
  name: "",
  level: "",
  start: "",
  end: "",
});

const handleSizeChange = (val: number) => {
  size.value = val;
};
const handleCurrentChange = (val: number) => {
  current.value = val;
};

const onSubmit = () => {
  getData();
};

const deleteTeacher = async (id: number) => {
  const res = await deleteById({ id });
  res.success && getData() && ElMessage.success("删除成功");
};

const getData = async () => {
  const params = form.value;
  const res = await getList({
    current: current.value,
    size: size.value,
    params,
  });
  total.value = res.data.total;
  tableData.value = res.data.rows;
};

watch(
  () => [size.value, current.value],
  () => {
    getData();
  }
);

onMounted(() => {
  getData();
});
</script>

<style lang="less" scoped>
.el-table {
  margin-bottom: 20px;
}
</style>